package in.muton.edushare.components.messaging.util;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.images.ProfileImage;
import in.muton.edushare.components.messaging.recievepanel.AssignmentMessagePanel;
import in.muton.edushare.components.messaging.recievepanel.FileUploadMessagePanel;
import in.muton.edushare.components.messaging.recievepanel.PollMessagePanel;
import in.muton.edushare.components.messaging.recievepanel.ScheduleMessagePanel;
import in.muton.edushare.components.messaging.recievepanel.UserMessagePanel;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.dao.GroupsDAO;
import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.dao.UsersDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;
import com.edushare.common.vo.interfaces.IFileUploadable;

public class MessageTypeParser {
	public static Panel getAppropriatePanel(IModel<Messages> message) {
		String str = message.getObject().getMessage();
		if (str.contains("<<<EDUSHAREMESSAGINGMETADATA")) {
			if (str.contains("SCHEDULE"))
				return new ScheduleMessagePanel("msg", message);
			if (str.contains("NEWUSER"))
				return new EmptyPanel("msg");
			if (str.contains("ASSIGNMENT")&& AssignmentMessagePanel.exists(str))
				return new AssignmentMessagePanel("msg", message);
			if(str.contains("POLL") && PollMessagePanel.exists(str))
				return new PollMessagePanel("msg", message);
			if(str.contains("FILEUPLOAD") /*&& PollMessagePanel.exists(str)*/)
				return new FileUploadMessagePanel("msg", message);
		} else
			return new UserMessagePanel("msg", message);
		return new EmptyPanel("msg");
	}
	
	public static boolean isVisible(IModel<Messages> message){
		String str = message.getObject().getMessage();
		if (str.contains("ASSIGNMENT")&& !AssignmentMessagePanel.exists(str))
			return false;
		if(str.contains("POLL") && !PollMessagePanel.exists(str))
			return false;
		if(str.contains("FILEUPLOAD") && !FileUploadMessagePanel.exists(str))
			return false;
		return true;
	}

	public static EduShareURLPanel getURLPanel(int id, int type,
			String panelId) {
		ORMInterface orm = EduShareSession.get().getSessionORMInterface();
		switch (type) {
		case Messages.RECEIVER_TYPE_UNIVERSITY:
			Universities univ = new UniversitiesDAO(orm).getUnivById(id);
			return new EduShareURLPanel(panelId,
					new CayenneModel<Universities>(univ));
		case Messages.RECEIVER_TYPE_COURSE:
			CourseInstances cI = new CourseInstancesDAO(orm).getCourseInstanceById(id);
			return new EduShareURLPanel(panelId, new CayenneModel<Courses>(cI
					.getCourse()));
		case Messages.RECEIVER_TYPE_DEPARTMENT:
			Departments dept = new DepartmentsDAO(orm).getDepartmentByPk(id);
			return new EduShareURLPanel(panelId, new CayenneModel<Departments>(
					dept));
		case Messages.RECEIVER_TYPE_USER:
			Users user = new UsersDAO(orm).getUserByPk(id);
			return new EduShareURLPanel(panelId, new CayenneModel<Users>(user));
		case Messages.RECEIVER_TYPE_GROUP:
			Groups g = new GroupsDAO(orm).getGroupById(id);
			return new EduShareURLPanel(panelId, new CayenneModel<Groups>(g));
		}
		return null;
	}

	public static IFileUploadable getObject(int id, int type){
		ORMInterface orm = EduShareSession.get().getSessionORMInterface();
		switch (type) {
		case Messages.RECEIVER_TYPE_UNIVERSITY:
			return new UniversitiesDAO(orm).getUnivById(id);
		case Messages.RECEIVER_TYPE_COURSE:
			return new CourseInstancesDAO(orm).getCourseInstanceById(id).getCourse();
		case Messages.RECEIVER_TYPE_DEPARTMENT:
			return new DepartmentsDAO(orm).getDepartmentByPk(id);
		case Messages.RECEIVER_TYPE_USER:
			return new UsersDAO(orm).getUserByPk(id);
		case Messages.RECEIVER_TYPE_GROUP:
			return new GroupsDAO(orm).getGroupById(id);
		}
		return null;
	}
	
	
	public static ProfileImage getImage(int id, int type, String panelId) {
		ORMInterface orm = EduShareSession.get().getSessionORMInterface();
		switch (type) {
		case Messages.RECEIVER_TYPE_UNIVERSITY:
			Universities univ = new UniversitiesDAO(orm).getUnivById(id);
			return new ProfileImage(panelId, new CayenneModel<IFileUploadable>(
					univ), ApplicationConstants.FILE_SUFFIX_ICON);
		case Messages.RECEIVER_TYPE_COURSE:
			CourseInstances cI = new CourseInstancesDAO(orm).getCourseInstanceById(id);
			return new ProfileImage(panelId, new CayenneModel<IFileUploadable>(
					cI.getCourse()), ApplicationConstants.FILE_SUFFIX_ICON);
		case Messages.RECEIVER_TYPE_DEPARTMENT:
			Departments dept = new DepartmentsDAO(orm).getDepartmentByPk(id);
			return new ProfileImage(panelId, new CayenneModel<IFileUploadable>(
					dept), ApplicationConstants.FILE_SUFFIX_ICON);
		case Messages.RECEIVER_TYPE_USER:
			Users user = new UsersDAO(orm).getUserByPk(id);
			return new ProfileImage(panelId,
					new CayenneModel<IFileUploadable>(user),
					ApplicationConstants.FILE_SUFFIX_ICON);
		case Messages.RECEIVER_TYPE_GROUP:
			Groups g = new GroupsDAO(orm).getGroupById(id);
			return new ProfileImage(panelId,
					new CayenneModel<IFileUploadable>(g),
					ApplicationConstants.FILE_SUFFIX_ICON);
		}
		return null;
	}

}
